package com.aspire.moss.portal;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Toast;

import com.aspire.moss.portal.view.AppInfo;
import com.aspire.moss.portal.view.ClipImageView;
import com.aspire.moss.portal.view.DragController;
import com.aspire.moss.portal.view.Workspace;
import com.aspire.moss.portal.view.WorkspaceAdapter;

public class Main extends Activity {

	private static final String TAG = "Main";

	private Context mContext;

	private Workspace workspace;

	DragController controller;
	
	ClipImageView clipImageView;


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return workspace.onTouchEvent(event);
	}

	public void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "Main : onCreate");
		mContext = getBaseContext();
		controller = new DragController(mContext);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		workspace = (Workspace) findViewById(R.id.workspace);
		clipImageView = (ClipImageView) findViewById(R.id.clipview);
		
		workspace.setWorkspaceListener(clipImageView);
		
		List<List<AppInfo>> apps = createDatasourre();
		WorkspaceAdapter adapter = new WorkspaceAdapter(mContext, apps);
		adapter.setItemClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(mContext, "我点了", Toast.LENGTH_SHORT).show();
			}
		});
		
		adapter.setItemLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				Toast.makeText(mContext, "我长按了", Toast.LENGTH_SHORT).show();
				Workspace.isDynamic = true;
				workspace.postInvalidate();
				return true;
			}
		});
		workspace.setWorkspaceAdapter(adapter);
		adapter.notifyObservers();
		workspace.setDragController(controller);
		
		
	}
	
	private List<List<AppInfo>> createDatasourre(){
		BitmapDrawable bitmapDrawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.comtencentqqphonebook);
		Bitmap bitmap = bitmapDrawable.getBitmap();
		
		int icon = (int) mContext.getResources().getDimension(R.dimen.icon);
		Bitmap app = Bitmap.createBitmap(icon, icon, Config.ARGB_8888);
		
		android.graphics.Matrix matrix = new Matrix();
		matrix.preScale(icon, icon);
		Canvas canvas = new Canvas(app);
		canvas.drawBitmap(bitmap, matrix, null);
		
		List<List<AppInfo>> apps =new ArrayList<List<AppInfo>>();
		List<AppInfo> page = null;
		Random random = new Random();
		AppInfo info = null;
		for(int i = 0 ;i < 7 ;i++){
			page = new ArrayList<AppInfo>();
			info = new AppInfo(i, 0, 0, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 1, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 2, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 0, 3, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 0, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 1, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 2, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 1, 3, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 0, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 1, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 2, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 2, 3, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 0, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 1, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 2, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			info = new AppInfo(i, 3, 3, bitmap , "程序" + random.nextInt(10));
			page.add(info);
			apps.add(page);
		}
		return apps;
	}

}
